package com.example.demo.controller;

import com.example.demo.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/userLogin")
    public String userLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("sessionId", session.getId());
        session.setAttribute("port", request.getLocalPort());
        return "login";
    }

    @RequestMapping("/loginSystem")
    public String loginSystem(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        Map userInfo = loginService.userInfo(userName, password);
        if (userInfo != null) {
            return "redirect:/login/index";
        }
        return "redirect:/login/userLogin";

    }

    @RequestMapping("/index")
    public String index() {
        return "result";
    }
}
